<?php

namespace Regard\ProductsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Brand
 *
 * @ORM\Table()
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="Regard\ProductsBundle\Entity\BrandRepository")
 */
class Brand
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @Assert\File(maxSize="6000000")
     * @ORM\Column(name="logo", type="string", length=255)
     */
    private $logo;

    /**
     * @var boolean
     *
     * @ORM\Column(name="optin", type="boolean", nullable=true)
     */
    private $toShow;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Brand
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set logo
     *
     * @param $logo
     * @return Brand
     */
    public function setLogo($logo)
    {
        $this->logo = $logo;
    
        return $this;
    }

    /**
     * Get logo
     *
     * @return string
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * Set toShow
     *
     * @param boolean $toShow
     * @return Brand
     */
    public function setToShow($toShow)
    {
        $this->toShow = $toShow;
    
        return $this;
    }

    /**
     * Get toShow
     *
     * @return boolean 
     */
    public function getToShow()
    {
        return $this->toShow;
    }
	
	public function getFullImagePath() {
        return null === $this->logo ? null : $this->getUploadRootDir(). $this->logo;
    }
 
    protected function getUploadRootDir() {
        // the absolute directory path where uploaded documents should be saved
        return __DIR__ . '/../../../../web/img/content/';
    }
 
    protected function getTmpUploadRootDir() {
        // the absolute directory path where uploaded documents should be saved
        return __DIR__ . '/../../../../web/upload/';
    }
 
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function uploadImage() {
        // the file property can be empty if the field is not required
        if (null === $this->logo) {
            return;
        }
        if(!$this->id){
            $this->logo->move($this->getTmpUploadRootDir(), $this->logo->getClientOriginalName());
        }else{
            $this->logo->move($this->getUploadRootDir(), $this->logo->getClientOriginalName());
        }
        $this->setLogo($this->logo->getClientOriginalName());
    }
     
    /**
     * @ORM\PostPersist()
     */
    public function moveImage()
    {
        if (null === $this->logo) {
            return;
        }
        if(!is_dir($this->getUploadRootDir())){
            mkdir($this->getUploadRootDir());
        }
        copy($this->getTmpUploadRootDir().$this->logo, $this->getFullImagePath());
        unlink($this->getTmpUploadRootDir().$this->logo);
    }
 
    /**
     * @ORM\PreRemove()
     */
    public function removeImage()
    {
        unlink($this->getFullImagePath());
        //rmdir($this->getUploadRootDir());
    }
}
